package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZhaoJun on 2021/11/24 12:06
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //A. 保存套餐基本信息
        this.save(setmealDto);
        System.out.println("看看setmealDto.getId："+setmealDto.getId());
        //B. 获取套餐关联的菜品集合，并为集合中的每一个元素赋值套餐ID(setmealId)
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().forEach((item)->{
            //传进来的setmealdto在保存到数据库中时，数据库生产了主键id并返回给了这个对象，所以这边能取出来
            item.setSetmealId(setmealDto.getId());
        });
        //C. 批量保存套餐关联的菜品集合
        setmealDishService.saveBatch(setmealDishes);

        return;



    }

    @Override
    public Page pageAndCondition(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,Setmeal::getName,name).orderByDesc(Setmeal::getUpdateTime);
        
        this.page(pageInfo,lqw);

        Page<SetmealDto> pageDato = new Page<>();

        BeanUtils.copyProperties(pageInfo,pageDato,"records");
        List<Setmeal> pageInfoRecords = pageInfo.getRecords();

        List<SetmealDto> setmealDtoList = pageInfoRecords.stream().map(c -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(c, setmealDto);
            Category one = categoryService.getById(c.getCategoryId());
            setmealDto.setCategoryName(one.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        pageDato.setRecords(setmealDtoList);

        return pageDato;


    }

    @Override
    public Boolean del(Long[] ids) {
        List<Long> list = Arrays.asList(ids);
//        ArrayList<Long> list = new ArrayList<>();
//        for (Long id : ids) {
//            list.add(id);
//        }
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);

        int count = this.count(lqw);
        if(count>0){
            return false;
        }

        this.removeByIds(list);

        LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(lqw2);

        return true;


    }

    @Override
    public SetmealDto findById(Long id) {
        Setmeal setmeal = this.getById(id);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> setmealDishList = setmealDishService.list(lqw);
        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal,setmealDto);

        setmealDto.setSetmealDishes(setmealDishList);

        return setmealDto;
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        Long setmealId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(lqw);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().forEach(c->
            c.setSetmealId(setmealId)

        );

        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    public void status(Integer status, Long[] ids) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,Arrays.asList(ids));
        this.update(setmeal,lqw);
    }
}
